package org.makabaka.wfwwb.service.Impl;

import com.alibaba.excel.EasyExcel;
import org.makabaka.wfwwb.mapper.*;
import org.makabaka.wfwwb.pojo.*;
import org.makabaka.wfwwb.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class UploadServiceImpl implements UploadService {

    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private TeamMemberMapper teamMemberMapper;

    @Autowired
    private AProblemMapper aProblemMapper;

    @Autowired
    private ProblemSelectionMapper problemSelectionMapper;

    @Autowired
    private InstructorMapper instructorMapper;

    @Autowired
    private JudgeMapper judgeMapper;

    @Autowired
    private AScoreMapper aScoreMapper;

    @Autowired
    private BScoreMapper bScoreMapper;

    @Override
    public String processExcel(String tableName, MultipartFile file) throws Exception {

        int a = 0;
        int b = 0;
        switch (tableName) {
            case "team":
                List<Team> teams = EasyExcel.read(file.getInputStream())
                        .head(Team.class)
                        .sheet()
                        .doReadSync();
                b = teams.size();
                for (Team team : teams) {
                    try {
                        int t = teamMapper.insert(team);
                        a += t;
                    } catch (Exception e) {
                        // 忽略插入失败的记录

                    }
                }
                break;

            case "teamMember":
                List<TeamMember> teamMembers = EasyExcel.read(file.getInputStream())
                        .head(TeamMember.class)
                        .sheet()
                        .doReadSync();
                b = teamMembers.size();
                for (TeamMember member : teamMembers) {
                    try {
                        int t = teamMemberMapper.insert(member);
                        a += t;
                    } catch (Exception e) {
                        // 忽略插入失败的记录
                    }
                }
                break;

            case "aProblem":
                List<AProblem> aProblems = EasyExcel.read(file.getInputStream())
                        .head(AProblem.class)
                        .sheet()
                        .doReadSync();
                b = aProblems.size();
                for (AProblem problem : aProblems) {
                    try {
                        int t = aProblemMapper.insert(problem);
                        a += t;
                    } catch (Exception e) {
                        // 忽略插入失败的记录
                    }
                }
                break;

            case "problemSelection":
                List<ProblemSelection> problemSelections = EasyExcel.read(file.getInputStream())
                        .head(ProblemSelection.class)
                        .sheet()
                        .doReadSync();
                b = problemSelections.size();
                for (ProblemSelection selection : problemSelections) {
                    try {
                        int t = problemSelectionMapper.insert(selection);
                        a += t;
                    } catch (Exception e) {
                        // 忽略插入失败的记录
                    }
                }
                break;

            case "instructor":
                List<Instructor> instructors = EasyExcel.read(file.getInputStream())
                        .head(Instructor.class)
                        .sheet()
                        .doReadSync();
                b = instructors.size();
                for (Instructor instructor : instructors) {
                    try {
                        int t = instructorMapper.insert(instructor);
                        a += t;
                    } catch (Exception e) {
                        // 忽略插入失败的记录
                    }
                }
                break;

            case "judge":
                List<Judge> judges = EasyExcel.read(file.getInputStream())
                        .head(Judge.class)
                        .sheet()
                        .doReadSync();
                b = judges.size();
                for (Judge judge : judges) {
                    try {
                        int t = judgeMapper.insert(judge);
                        a += t;
                    } catch (Exception e) {
                        // 忽略插入失败的记录
                    }
                }
                break;

            case "aScore":
                List<AScore> aScores = EasyExcel.read(file.getInputStream())
                        .head(AScore.class)
                        .sheet()
                        .doReadSync();
                b = aScores.size();
                for (AScore score : aScores) {
                    try {
                        int t = aScoreMapper.insert(score);
                        a += t;
                    } catch (Exception e) {
                        // 忽略插入失败的记录
                    }
                }
                break;

            case "bScore":
                List<BScore> bScores = EasyExcel.read(file.getInputStream())
                        .head(BScore.class)
                        .sheet()
                        .doReadSync();
                b = bScores.size();
                for (BScore score : bScores) {
                    try {
                        int t = bScoreMapper.insert(score);
                        a += t;
                    } catch (Exception e) {
                        // 忽略插入失败的记录
                    }
                }
                break;

            default:
                throw new IllegalArgumentException("不支持的表名: " + tableName);
        }
        if (a == 0) throw new Exception("未能插入任何一条数据！");
        return "[" + a + "/" + b + "]";
    }
}